// const moment = require('./moment.js');
const moment  = require('moment');
const timeStampFormat = (timeStamp,format)=>{
  var date = new Date((typeof timeStamp == Number) ? timeStamp : parseInt(timeStamp));
  return moment(date).format(format?format:'YYYY-MM-DD HH:mm:ss');
}
const dateObjFormat = (date = new Date(),format = "YYYY-MM-DD HH:mm:ss")=>{
  return moment(date).format(format);
}
const lastDay = (date = new Date())=>{
  return moment(date).add(-1,'d');
}

const timeStampCompare = (before,after) => {
  //返回时间比较;比如几分钟前；几小时前；几个月前等；
  //时间戳单位是毫秒；
  var deltaMins = (after-before)/ (1000*60);
  //得到差距分
  var mins = 1;//1分钟

  //小时
  var hour = mins * 60;

  //天
  var day = hour * 24;

  var threeDay = day * 3;

  var month = day * 30;

  var string = '';
  var delta = 0;

  //小于1分钟
  if (deltaMins < mins) {
    delta = 0;
    string = '刚刚'
  } else if (mins <= deltaMins && deltaMins < hour) {
    delta = parseInt(deltaMins / mins);
    string =  delta+ '分钟前';
  } else if (hour <= deltaMins && deltaMins < day) {
    delta = parseInt(deltaMins / hour);
    string = delta + '小时前';
  } else if (day <= deltaMins && deltaMins < threeDay) {

    delta = parseInt(deltaMins / day);
    string = delta + '天前';
  } else if (threeDay <= deltaMins && deltaMins < month) {
    string = timeStampFormat(before,'YYYY/MM/DD') 
  } else {
    string = timeStampFormat(before, 'YYYY/MM/DD') 
  }
  return {
    string:string,
    delta:delta
  }
}


module.exports = {
  timeStampFormat:timeStampFormat,
  timeStampCompare: timeStampCompare,
  dateObjFormat:dateObjFormat,
  lastDay:lastDay,
  moment
}